package edu.upol.paradise.hypervisor.protocol;

import org.apache.log4j.Logger;

public class ServicePacket extends Packet {
	static final Logger log = Logger.getLogger(ServicePacket.class);
	
	public static ServicePacket asServicePacket(Packet packet){
		String header = packet.getHeader();
		if (isServicePackerHeader(header)){
			
			ServicePacket np = createServicePacket(header, packet.getPacketData());
			np.setHeader(header);
			np.setPacketData(packet.getPacketData());
			return np;
		} else
			return null;
	}
	
	private static ServicePacket createServicePacket(String header,
			String packetData) {
		if (PacketHeaders.PING.equals(header))
			return new PingPacket(packetData);
		if (PacketHeaders.PING_REPLY.equals(header))
			return new PongPacket(packetData);
		if (PacketHeaders.REQUEST_CLUSTER_NODE_LIST.equals(header))
			return new RequestCNLPacket(packetData);
		if (PacketHeaders.SEND_CLUSTER_NODE_LIST.equals(header))
			return new SendCNLPacket(packetData);
		return null;
	}

	private static boolean isServicePackerHeader(String header) {
		boolean isService = false;
		isService = isService || PacketHeaders.PING.equals(header);
		isService = isService || PacketHeaders.PING_REPLY.equals(header);
		isService = isService || PacketHeaders.REQUEST_CLUSTER_NODE_LIST.equals(header);
		isService = isService || PacketHeaders.SEND_CLUSTER_NODE_LIST.equals(header);
		return isService;
	}

	protected ServicePacket() {
		
	}
	
	
}
